package scu.maqiang.cagd;

import scu.maqiang.mesh.Tecplot;
import scu.maqiang.numeric.MVO;

import java.util.Arrays;

public class BezierCurves {
    public static double[] value(double[] Tx, double[] Ty, double u) {
        int m1 = Tx.length;
        double[][] Xmid = new double[m1][];
        double[][] Ymid = new double[m1][];
        Xmid[0] = Tx.clone();
        Ymid[0] = Ty.clone();
        for(int i = 1; i < m1; i++) {
            Xmid[i] = new double[m1 - i];
            Ymid[i] = new double[m1 - i];
            for(int j = 0; j < m1 - i; j++) {
                Xmid[i][j] = (1 - u) * Xmid[i - 1][j] + u * Xmid[i - 1][j + 1];
                Ymid[i][j] = (1 - u) * Ymid[i - 1][j] + u * Ymid[i - 1][j + 1];
            }
        }
        //System.out.println(MVO.toString(Xmid));
        //System.out.println(MVO.toString(Ymid));
        return new double[]{Xmid[m1 - 1][0], Ymid[m1 - 1][0]};
    }
    public static void main(String[] args) {
        double[] x = {0, 1.0000, 2.0000, 3.0000, 3.5000, 0, 1.0000, 2.0000, 3.0000, 3.5000};
        double[] y = {0, 2.5000, 3.0000, 1.5000, 0, 0, 2.5000, 3.0000, 1.5000, 0};
        double[] coords = MVO.linspace(0, 1, 101);
        double[] curvesX = new double[101];
        double[] curvesY = new double[101];
        for(int i = 0; i < 101; i++) {
            double[] points = value(x, y, coords[i]);
            curvesX[i] = points[0];
            curvesY[i] = points[1];
        }
        double[][] Y = {curvesY};
        System.out.println(MVO.toString(curvesX));
        Tecplot.LineXY("BezierCurves.dat", curvesX, Y);
    }
}
